import Vue from 'vue'
import Router from 'vue-router'
import Donate from '@/components/Publish/Donate'
import Need from '@/components/Publish/Need'
Vue.use(Router)
const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/Donate', component: Donate },
        { path: '/Need', component: Need },
      ]
    },

  ],
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 表示从哪个路径跳转而来
  // next 是一个放行函数，表示放行

  if (to.path === '/login') next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) next('/login')
  next()
})
export default router
